package com.wn.ticket.web.backstage;

import com.wn.ticket.common.JwtUtils;
import com.wn.ticket.common.PageBean;
import com.wn.ticket.common.ResponseEntity;
import com.wn.ticket.entity.User;
import com.wn.ticket.service.UserService;
import com.wn.ticket.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;

/**
 * @author wuqingting
 * @date 2022/8/27
 */
@RestController
@RequestMapping("/backstage/user")
public class UserController {
    @Autowired
    private UserService userService;

//    @RequestMapping("/login")
//    public ResponseEntity login(@RequestBody User user) {
//        User user1 = userService.login(user);
//        if (user1 != null) {
//            return ResponseEntity.success;
//        }
//
//        return ResponseEntity.fail;
//    }

    @RequestMapping("/add")
    public ResponseEntity add(@RequestBody User user) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));//将密码加密
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取系统时间
        String rt = df.format(System.currentTimeMillis());
        user.setRegistTime(rt);
        userService.add(user);

        return ResponseEntity.success;
    }

    @Autowired
    private JwtUtils jwtUtils;

    @RequestMapping("/get")
    public ResponseEntity get(HttpServletRequest request) {
        String token = request.getHeader("token");
        String account = (String) jwtUtils.getInfo(token, "account");
        return new ResponseEntity(account);
    }

    @PreAuthorize("hasAnyAuthority('super')")
    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody User user) {
        userService.update(user);
        return ResponseEntity.success;
    }


    @RequestMapping("/getPage")
    public ResponseEntity<User> getPage(@RequestBody UserVo userVo) {
        PageBean<User> pageBean = userService.getPage(userVo.getUser(), userVo.getCurrPage(), userVo.getPageSize());
        return new ResponseEntity(pageBean);
    }

    @PreAuthorize("hasAnyAuthority('super')")
    @RequestMapping("/deleted/{id}")
    public ResponseEntity<User> deleted(@PathVariable int id) {
        User user = new User();
        user.setId(id);
        user.setDeleted("y");
        userService.update(user);
        return ResponseEntity.success;
    }

    @PreAuthorize("hasAnyAuthority('super')")
    @RequestMapping("/undeleted/{id}")
    public ResponseEntity<User> undeleted(@PathVariable int id) {
        User user = new User();
        user.setId(id);
        user.setDeleted("n");
        userService.update(user);
        return ResponseEntity.success;
    }
}
